package com.kk.util.test.thread.wait;

/**
 * @function:
 * @author: kevin
 * @date: 2018-09-07 11:36
 */
public class ThreadWait1 implements Runnable {

    private String name;
    private Object prev;
    private Object self;
    public ThreadWait1(String name, Object prev, Object self) {
        this.name = name;
        this.prev = prev;
        this.self = self;
    }
    @Override
    public void run() {
        int count =10;
        while (count > 0) {
            synchronized (prev) {
                synchronized (self) {
                    System.out.print(name);
                    count--;
                    self.notify();
                }
                try {
                    prev.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();
        Thread t1 = new Thread(new ThreadWait1("A", c, a));
        Thread t2 = new Thread(new ThreadWait1("B", a, b));
        Thread t3 = new Thread(new ThreadWait1("C", b, c));
        t1.start();
        t1.sleep(100);
        t2.start();
        t2.sleep(100);
        t3.start();
        t3.sleep(100);
    }
}
